package com.artPreter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.artPreter.mapper.ContentPictureRelativeMapper;
import com.artPreter.pojo.ContentPictureRelative;
import com.artPreter.pojo.Picture;
import com.artPreter.mapper.PictureMapper;
import com.artPreter.service.IPictureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 图片表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-21
 */
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements IPictureService {

    @Autowired
    private ContentPictureRelativeMapper contentPictureRelativeMapper;

    @Override
    public IPage<Picture> getPage(Integer currentPage, Integer size, Picture picture) {
        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Picture::getStatus,1);
        queryWrapper.orderByDesc(Picture::getId);
        // 内容id
        if(picture.getPlateId()!=null&&!"".equals(picture.getPlateId())){
            queryWrapper.eq(Picture::getPlateId,picture.getPlateId());
        }
        if(picture.getPlateType()!=null&&!"".equals(picture.getPlateType())){
            queryWrapper.eq(Picture::getPlateType,picture.getPlateType());
        }


        IPage<Picture> pictureIPage = new Page<>(currentPage,size);
        pictureIPage = this.baseMapper.selectPage(pictureIPage,queryWrapper);
        return pictureIPage;
    }

    @Override
    public IPage<Picture> getPageNoRelative(Integer currentPage, Integer size, Picture picture) {
        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Picture::getStatus,1);
        queryWrapper.orderByDesc(Picture::getId);
        queryWrapper.eq(Picture::getPlateId,picture.getPlateId());
        // 内容id
        String contentId = picture.getContentId();
        queryWrapper.notExists("SELECT 1 FROM jv_content_picture_relative WHERE jv_content_picture_relative.content_id = '"+contentId+"' and jv_content_picture_relative.picture_id = jv_picture.picture_id");
        IPage<Picture> pictureIPage = new Page<>(currentPage,size);

        // 图片名字模糊搜索
        if(picture.getName()!=null&&!"".equals(picture.getName())){
            queryWrapper.like(Picture::getName,picture.getName());
        }

        pictureIPage = this.baseMapper.selectPage(pictureIPage, queryWrapper);
        return pictureIPage;
    }

    @Override
    public IPage<Picture> getPageRelative(Integer currentPage, Integer size, Picture picture) {
        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Picture::getStatus,1);
        queryWrapper.orderByDesc(Picture::getId);
        queryWrapper.eq(Picture::getPlateId,picture.getPlateId());
        // 内容id
        String contentId = picture.getContentId();
        queryWrapper.exists("SELECT 1 FROM jv_content_picture_relative WHERE jv_content_picture_relative.content_id = '"+contentId+"' and jv_content_picture_relative.picture_id = jv_picture.picture_id");
        IPage<Picture> pictureIPage = new Page<>(currentPage,size);

        // 图片名字模糊搜索
        if(picture.getName()!=null&&!"".equals(picture.getName())){
            queryWrapper.like(Picture::getName,picture.getName());
        }


        pictureIPage = this.baseMapper.selectPage(pictureIPage, queryWrapper);
        return pictureIPage;
    }

    @Override
    public void removePictureRelative(Picture picture) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(picture.getPictureId()!=null&&!"".equals(picture.getPictureId())){
            queryWrapper.eq("picture_id",picture.getPictureId());
        }else {
            return;
        }
        if(picture.getContentId()!=null&&!"".equals(picture.getContentId())){
            queryWrapper.eq("content_id",picture.getContentId());
        }else {
            return;
        }
        contentPictureRelativeMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByArtistId(String artistId) {
        if(artistId!=null&&!"".equals(artistId)){
            List<Picture> pictureList = this.baseMapper.selectList(new LambdaQueryWrapper<Picture>().eq(Picture::getPlateId,artistId));
            for (Picture picture : pictureList) {
                this.delete(picture);
            }
        }
    }

    @Override
    public void addToRelative(Picture picture) {
        ContentPictureRelative contentPictureRelative = new ContentPictureRelative();
        if(picture.getPictureId()!=null&&!"".equals(picture.getPictureId())){
            contentPictureRelative.setPictureId(picture.getPictureId());
        }else {
            return;
        }
        if(picture.getContentId()!=null&&!"".equals(picture.getContentId())){
            contentPictureRelative.setContentId(picture.getContentId());
        }else {
            return;
        }
        contentPictureRelativeMapper.insert(contentPictureRelative);
    }



    @Override
    public void add(Picture picture) {
        String uuid = UUID.randomUUID().toString();
        picture.setPictureId(uuid);
        this.baseMapper.insert(picture);
    }

    @Override
    public void edit(Picture picture) {
        this.baseMapper.updateById(picture);
    }

    @Override
    public void delete(Picture picture) {
        if(picture.getId()==null){
            return;
        }
        LambdaQueryWrapper<Picture> pictureLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pictureLambdaQueryWrapper.eq(Picture::getId,picture.getId());
        List<Picture> pictureList = this.baseMapper.selectList(pictureLambdaQueryWrapper);
        if(pictureList.size()>0){
            if(pictureList.get(0).getPictureId()!=null){
                // 删除图片关联表
                LambdaQueryWrapper<ContentPictureRelative> pictureRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
                pictureRelativeLambdaQueryWrapper.eq(ContentPictureRelative::getPictureId,pictureList.get(0).getPictureId());
                contentPictureRelativeMapper.delete(pictureRelativeLambdaQueryWrapper);
            }
            this.baseMapper.deleteById(pictureList.get(0).getId());
        }
    }
}
